/**
 * 登录按钮控件
 */
var LoginBtnUnit = Unit.extend({
    /**
     * 屏蔽登录点击
     * @type {Boolean}
     * @private
     */
    _loginClose: false,
    /**
     * 接收code的key
     * @type {String}
     * @private
     */
    _reciveCodeKey: null,
    /**
     * auth请求
     * @type {String}
     * @private
     */
    _authReq: null,

    initUnit: function () {
        if (this._reciveCodeKey) {
            this._addEventListener(this._reciveCodeKey, this._receiveCode.bind(this));
        }
    },

    _initClickListeners: function () {
        this._super();
        this._clickListeners = {
            'btn': '_click'
        };
    },
    /**
     * 点击事件
     * @protected
     */
    _click: function () {
        if (this._loginClose) {
            return;
        }
        this._waitLoginClick();
        var loginBoard = this.getParent().getParent().getParent().getParent();
        if (!(loginBoard instanceof LoginBoard)) {
            cc.error('登录按钮父节点不是LoginBoard');
            return;
        }
        if (!loginBoard.agreeXieYi()) {
            showAlert('请先同意用户协议!');
            return;
        }
        if (!this._isInstalled()) {
            this.showNoInstalledAlert();
            return;
        }
        this.redirectLogin();
    },
    /**
     * 开始登录
     * @protected
     */
    redirectLogin: function () {
    },
    /**
     * 接收到code
     * @param code
     * @protected
     */
    _receiveCode: function (code) {
    },
    /**
     * 执行登录
     * @param response
     * @param loginType
     */
    doLogin: function (response, loginType) {
        if (!response) {
            return;
        }
        showLoading('正在登录...');
        var userInfo = response;
        gameData.loginType = loginType || 'yk';
        gameData.clientId = userInfo.openid || userInfo.unionid;
        gameData.nickname = userInfo.nickname;
        gameData.sex = userInfo.sex || 1;
        gameData.province = userInfo.province;
        gameData.city = userInfo.city;
        gameData.country = userInfo.country;
        gameData.headimgurl = userInfo.headimgurl || res.defaultHead;
        gameData.unionid = userInfo.unionid;
        eventManager.dispatchEvent(EventKey.DoLogin);
    },
    /**
     * 是否安装
     * @returns {boolean}
     * @protected
     */
    _isInstalled: function () {
        return true;
    },
    /**
     * 未安装提示
     */
    showNoInstalledAlert: function () {
        showAlert('您没安装!');
    },
    /**
     * 控制登录按钮屏蔽
     * @private
     */
    _waitLoginClick: function () {
        var self = this;
        self._loginClose = true;
        setTimeout(function () {
            self._loginClose = false;
        }, 20);
    },
    /**
     * 请求auth
     * @protected
     */
    _requestAuth: function (code, key, data) {
        if (!this._authReq) {
            showAlert('没有设置请求!');
            return;
        }
        var time = getCurTimeMillisecond();
        var sign = Crypto.MD5('request:/' + app.authPath + '/' + this._authReq + '?Code=' + code + '&time=' + time);
        var url = 'http://auth.yygameapi.com:44440/' + app.authPath + '/' + this._authReq + '?Code=' + code + '&time=' + time + '&sign=' + sign + '';
        if (key && data) {
            sign = Crypto.MD5('request:/' + app.authPath + '/' + this._authReq + '?Code=' + code + "&time=" + time + "&" + key + "=" + data);
            url = 'http://auth.yygameapi.com:44440/' + app.authPath + '/' + this._authReq + '?Code=' + code + '&time=' + time + "&" + key + "=" + data + '&sign=' + sign + '';
        }
        // console.log(url);
        // console.log(sign);
        http.get(url, this._requestAuthSuccess.bind(this), this._requestAuthFailure.bind(this));
    },
    /**
     * 请求授权失败
     * @private
     */
    _requestAuthFailure: function () {
        hideLoading();
    },
    /**
     * 请求授权成功
     * @protected
     */
    _requestAuthSuccess: function (response) {
    }
});